﻿#region
/* ===============================================
 * 作者：guopeng           时间：2012/3/24 11:26:02
 * 
 * 
 * 类描述：
 * 
 * 
 * =============================================== */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ueEngine.Biz.Inf.LogicEntity
{
    public class ItemListBox<T> where T : class
    {
        public ItemListBox(){}
        public ItemListBox(List<T> list)
        {
            if (list == null || list.Count(c => c != null) == 0)
            {
                this.Data = new List<T>();
            }
            else
            {
                this.Data = list;
            }
        }
        public List<T> Data { get; set; }
        public int ItemTotle { get; set; }
        public int TotlePage { get; set; }
        public int CurrentPage { get; set; }
        public int PrePage { get; set; }
        public int NextPage { get; set; }
        public string PageItemSegment { get; set; }

        public ItemListBox<T> BuildPage(int itemCount, int currentPage, int pageSize)
        {
            if (itemCount <= 0)
            {
                ItemTotle = 0;
                TotlePage = 0;
                CurrentPage = 0;
                PrePage = 0;
                NextPage = 0;
                return this;
            }
            ItemTotle = itemCount;
            this.TotlePage = itemCount % pageSize == 0 ? itemCount / pageSize : itemCount / pageSize + 1;
            if (this.TotlePage > 0)
            {
                this.CurrentPage = this.CurrentPage < 0 ? 0 : this.CurrentPage;
                this.CurrentPage = this.CurrentPage > this.TotlePage - 1 ? this.TotlePage - 1 : currentPage;
            }
            else
            {
                this.CurrentPage = 0;
            }
            this.PrePage = this.CurrentPage == 0 ? 0 : this.CurrentPage - 1;
            this.NextPage = this.CurrentPage == this.TotlePage - 1 ? this.TotlePage - 1 : this.CurrentPage + 1;
            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="itemCount"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="hrefisscript"></param>
        /// <param name="scriptfm">如果href是javascript方法，则这里是方法的format串</param>
        /// <param name="param_page">如果href不是javascript方法，则这里是page的查询参数</param>
        /// <returns></returns>
        public ItemListBox<T> BuildPage(int itemCount, int currentPage, int pageSize, PageParameter setting)
        {
            string requestPath = System.Web.HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + System.Web.HttpContext.Current.Request.PathInfo;
            BuildPage(itemCount, currentPage, pageSize);
            StringBuilder sb = new StringBuilder();
            string urlformat = string.Empty;
            string rawurl = System.Web.HttpContext.Current.Request.RawUrl;
            if (!setting.IsActive)
            {
                if (rawurl.IndexOf('?') >= 0)
                {
                    if (rawurl.IndexOf(setting.Param_page + "=" + currentPage.ToString()) > 0)
                    {
                        urlformat = rawurl.Replace(setting.Param_page + "=" + currentPage.ToString(), setting.Param_page + "={0}");
                    }
                    else
                    {
                        urlformat = rawurl + "&" + setting.Param_page + "={0}";
                    }
                }
                else
                {
                    if (rawurl.IndexOf(setting.Param_page + "=" + currentPage.ToString()) > 0)
                    {
                        urlformat = rawurl.Replace(setting.Param_page + "=" + currentPage.ToString(), setting.Param_page + "={0}");
                    }
                    else
                    {
                        if (urlformat.IndexOf('-') >= 0)
                        {
                            urlformat = rawurl + "_" + setting.Param_page + "={0}";
                        }
                        else
                        {
                            urlformat = rawurl + "-" + setting.Param_page + "={0}"; 
                        }
                    }
                }
                
            }
            else
            {
                urlformat = setting.Script;
            }
            if (currentPage <= 5)
            {
                for (int i = 0, len = TotlePage > 10 ? 10 : TotlePage; i < len; i++)
                {
                    if (currentPage == i)
                    {
                        if (setting.IsActive)
                        {
                            sb.Append(string.Format("<div class=\"joblist-pages-item-current\"><a href=\"javascript:;\" onclick=\"" + urlformat + "\">{1}</a></div>", i, i + 1));
                        }
                        else
                        {
                            sb.Append(string.Format("<div class=\"joblist-pages-item-current\"><a href=\"" + urlformat + "\">{1}</a></div>", i, i + 1));
                        }
                    }
                    else
                    {
                        if (setting.IsActive)
                        {
                            sb.Append(string.Format("<div class=\"joblist-pages-item\"><a href=\"javascript:;\" onclick=\"" + urlformat + "\">{1}</a></div>", i, i + 1));
                        }
                        else
                        {
                            sb.Append(string.Format("<div class=\"joblist-pages-item\"><a href=\"" + urlformat + "\">{1}</a></div>", i, i + 1));
                        }
                    }
                }
            }
            else
            {
                if (setting.IsActive)
                {
                    sb.Append(string.Format("<div class=\"joblist-pages-item\"><a href=\"javascript:;\" onclick=\"" + urlformat + "\">{1}</a></div>", 0, 1));
                }
                else
                {
                    sb.Append(string.Format("<div class=\"joblist-pages-item\"><a href=\"" + urlformat + "\">{1}</a></div>", 0, 1));
                }
                sb.Append("<div class=\"joblist-pages-item1\">...</div>");
                for (int i = currentPage - 3, len = currentPage + 5 > TotlePage ? TotlePage : currentPage + 5;
                    i < len; i++)
                {
                    if (currentPage == i)
                    {
                        if (setting.IsActive)
                        {
                            sb.Append(string.Format("<div class=\"joblist-pages-item-current\"><a href=\"javascript:;\" onclick=\"" + urlformat + "\">{1}</a></div>", i, i + 1));
                        }
                        else
                        {
                            sb.Append(string.Format("<div class=\"joblist-pages-item-current\"><a href=\"" + urlformat + "\">{1}</a></div>", i, i + 1));
                        }
                    }
                    else
                    {
                        if (setting.IsActive)
                        {
                            sb.Append(string.Format("<div class=\"joblist-pages-item\"><a href=\"javascript:;\" onclick=\"" + urlformat + "\">{1}</a></div>", i, i + 1));
                        }
                        else
                        {
                            sb.Append(string.Format("<div class=\"joblist-pages-item\"><a href=\"" + urlformat + "\">{1}</a></div>", i, i + 1));
                        }
                    }
                }
            }
            PageItemSegment = sb.ToString();
            return this;
        }
    }
    public class PageParameter
    {
        /// <summary>
        /// 是否是使用onclick进行翻页
        /// </summary>
        public bool IsActive = false;
        /// <summary>
        /// 如果IsActive为true，则用户点击翻页时执行的函数
        /// </summary>
        public string Script = string.Empty;
        /// <summary>
        /// 用于翻页的Querystring参数，默认为"page"
        /// </summary>
        public string Param_page = "page";
    }
}
